﻿using GalaSoft.MvvmLight;

namespace Budgetsch.WP7.ViewModel.Cost
{
    using System;
    using System.Collections.ObjectModel;

    using Budgetsch.WP7.Navigation;
    using Budgetsch.WP7.ViewModel.Comment;
    using Budgetsch.WP7.WP7DataService;

    using GalaSoft.MvvmLight.Messaging;

    using CostComment = Budgetsch.WP7.Model.CostComment;

    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class CostDetailsViewModel : ViewModelBase
    {
        /// <summary>
        /// The <see cref="Cost" /> property's name.
        /// </summary>
        public const string CostPropertyName = "Cost";

        private Cost _Cost = null;

        /// <summary>
        /// Sets and gets the Cost property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public Cost Cost
        {
            get
            {
                return _Cost;
            }

            set
            {
                if (_Cost == value)
                {
                    return;
                }

                _Cost = value;
                RaisePropertyChanged(CostPropertyName);
            }
        }

        /// <summary>
        /// The <see cref="CommentViewModel" /> property's name.
        /// </summary>
        public const string CommentViewModelPropertyName = "CommentViewModel";

        private CommentViewModel _commentViewModel = null;

        /// <summary>
        /// Sets and gets the CommentViewModel property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public CommentViewModel CommentViewModel
        {
            get
            {
                return _commentViewModel;
            }

            set
            {
                if (_commentViewModel == value)
                {
                    return;
                }

                _commentViewModel = value;
                RaisePropertyChanged(CommentViewModelPropertyName);
            }
        }
        
        /// <summary>
        /// Initializes a new instance of the CostDetailsViewModel class.
        /// </summary>
        public CostDetailsViewModel()
        {

            Messenger.Default.Register < LoadCostDetailsViewMessage>(this,m=>
                {
                    Cost = m.Cost;
                    CommentViewModel = new CommentViewModel(Cost.CostComments);
                });

            Messenger.Default.Register<RefreshCostDetailsViewMessage>(
                this,
                m => this.CommentViewModel.CommentDetailsViewModels.Add(new CommentDetailsViewModel(m.Comment)));

            if (IsInDesignModeStatic)
            {
                Cost = new Cost
                    {
                        Amount = 3000,
                        IsRecurring = true,
                        StartDate = new DateTime(2011, 5, 11),
                        EndDate = new DateTime(2023, 11, 11),
                        Delta = 4,
                        CostCategory = new CostCategory { Name = "Fizetés" },
                        Description = "BME projekt pénz",
                        CostComments =
                            new ObservableCollection<WP7DataService.CostComment>
                                {
                                    new WP7DataService.CostComment
                                        { Comment = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi auctor leo pretium nibh imperdiet id rhoncus ante cursus. Praesent et sapien erat, sit amet tempus eros. Aliquam vel risus sed risus egestas molestie. Sed eget fermentum dui. Nunc ac massa ut dolor rhoncus ultrices in at urn", User = new User { UserName = "Sherrif" } },
                                    new WP7DataService.CostComment
                                        { Comment = "Fuuuu komment", User = new User { UserName = "Dyer" } },
                                    new WP7DataService.CostComment
                                        { Comment = "Csak a J", User = new User { UserName = "Jocó" } },
                                    new WP7DataService.CostComment
                                        { Comment = "Le derpina komment", User = new User { UserName = "Ákosch" } }
                                }
                    };
                CommentViewModel=new CommentViewModel(Cost.CostComments);
            }
        }
    }
}